package com.igeek;

import com.igeek.dao.DepDao;
import com.igeek.dao.EmpDao;
import com.igeek.entity.Department;
import com.igeek.entity.Employee;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.Reader;

/**
 * 多表映射
 */
public class Demo3 {

    public static void main(String[] args) throws Exception{
        //读取配置文件的输入流
        Reader reader = Resources.getResourceAsReader("mybatis.xml");
        //创建builder
        SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
        //构建sessionFactory
        SqlSessionFactory factory = builder.build(reader);
        //打开session
        SqlSession session = factory.openSession();

        EmpDao empDao = session.getMapper(EmpDao.class);
//        Employee emp = empDao.findById(100);
//        System.out.println(emp.getEmpName());
//        System.out.println(emp.getHireDate());
//        System.out.println(emp.getSalary());
//        System.out.println(emp.getDepartment().getDepId());

        DepDao depDao = session.getMapper(DepDao.class);
        Department dep = depDao.findById(50);
        System.out.println(dep.getDepId());
        System.out.println(dep.getDepName());
//        System.out.println(dep.getEmpList());
        System.out.println("-----------emps------------");
        for (Employee employee : dep.getEmpList()) {
            System.out.println(employee.getEmpId()+"\t"+employee.getEmpName()+"\t"+employee.getSalary()+"\t"+employee.getDepartment().getDepName());
        }

//        Employee emp = empDao.findByName("Donald");
//        System.out.println(emp.getSalary());

        session.close();

    }
}
